/**
 * 
 */
package com.nari.baseinfo.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.ICity;
import com.nari.baseinfo.service.ICityService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.components.dhtmltree.DhtmlTreeNode;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class CityAction extends JSONActionSupport {
	private static final long serialVersionUID = 7190257886715110320L;
	private ICityService iCityService;
	//行政机构查询
	private String parentID;
	private String cityName;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<ICity> cityPage;
	//行政信息管理
	private String cityID;
	private ICity city;
	private List<ICity> rootCityList;
	private List<ICity> childCityList;
	
	/**
	 * 行政单位信息管理导航
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-26
	 */
	public String queryCityMgrNav() throws Exception {
		//便于系统参数携带
		return "cityMgrNav";
	}
	
	/**
	 * 查询一级行政机构，并将其转化为树节点
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public String queryRootCityTree() throws Exception {
		//查询一级行政机构
		List<ICity> rootCityList = this.iCityService.queryFirstLevelCity();
		//构造动态树虚拟头节点
		DhtmlTreeNode rootNode = new DhtmlTreeNode();
		rootNode.setId("0");
		rootNode.setText("0");
		//构造城市树节点
//		DhtmlTreeNode leaderNode = new DhtmlTreeNode();
//		leaderNode.setId("行政机构树");
//		leaderNode.setText("行政机构树");
//		rootNode.getItem().add(leaderNode);
		List<DhtmlTreeNode> rootCityNodes = this.iCityService.formatCityListToTreeNodes(rootCityList);
		rootNode.getItem().addAll(rootCityNodes);
		this.writeJsonStrToResponse(JSONUtil.serialize(rootNode));
		return Action.SUCCESS;
	}
	
	/**
	 * 根据父机构查询子机构树节点
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public String queryCityTreeByParent() throws Exception {
		//查询父机构
		ICity pCity = this.iCityService.queryEntityById(parentID);
		//查询子机构
		List<ICity> childCityList = this.iCityService.queryCityByParentID(parentID);
		//构造机构树
		DhtmlTreeNode rootNode = this.iCityService.formatCityListToTreeNodes(Arrays.asList(new ICity[]{pCity})).get(0);
		List<DhtmlTreeNode> childNodes = this.iCityService.formatCityListToTreeNodes(childCityList);
		rootNode.getItem().clear();
		rootNode.getItem().addAll(childNodes);
		this.writeJsonStrToResponse(JSONUtil.serialize(rootNode));
		return Action.SUCCESS;
	}
	
	/**
	 * 分页查询行政机构列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public String queryCityPage() throws Exception {
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//根据分页信息查询机构列表
		this.cityPage = this.iCityService.queryCityPageByParentID(parentID, cityName, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.cityPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.cityPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "cityList";
	}
	
	/**
	 * 行政机构信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public String queryCityForManage() throws Exception {
		if(cityID!=null && !"".equals(cityID.trim())) {
			this.city = this.iCityService.queryEntityById(cityID);
		}
		return "cityManage";
	}
	
	/**
	 * 新增或修改机构信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public String saveOrUpdateCity() throws Exception {
		this.rmsg = this.iCityService.saveOrUpdateCity(city);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 根据机构唯一标识删除机构信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-5
	 */
	public String deleteCityByID() throws Exception {
		int counter = this.iCityService.countCityByParent(cityID, null);
		if(counter > 0) {
			this.rmsg = ResponseMsg.getInstance(false, "由于存在子机构，此机构无法被删除！");
			this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
			return Action.SUCCESS;
		}
		this.iCityService.deleteEntityById(cityID);
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}
	
	/**
	 * 通过父节点查询子机构列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-21
	 */
	public String queryChildCitysByParentUID() throws Exception {
		this.childCityList = this.iCityService.queryCityByParentID(parentID);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.childCityList));
		return Action.SUCCESS;
	}
	
	/**
	 * 机构树导航
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-15
	 */
	public String naveToCityTree() throws Exception {
		//携带权限控制参数等信息
		return "cityTreeNav";
	}
	
	/**
	 * @return the iCityService
	 */
	@JSON(serialize=false)
	public ICityService getiCityService() {
		return iCityService;
	}
	
	/**
	 * @param iCityService the iCityService to set
	 */
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}

	/**
	 * @return the parentID
	 */
	public String getParentID() {
		return parentID;
	}

	/**
	 * @param parentID the parentID to set
	 */
	public void setParentID(String parentID) {
		this.parentID = parentID;
	}

	/**
	 * @return the cityName
	 */
	public String getCityName() {
		return cityName;
	}

	/**
	 * @param cityName the cityName to set
	 */
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the cityPage
	 */
	public ResultWithPageCond<ICity> getCityPage() {
		return cityPage;
	}

	/**
	 * @param cityPage the cityPage to set
	 */
	public void setCityPage(ResultWithPageCond<ICity> cityPage) {
		this.cityPage = cityPage;
	}

	/**
	 * @return the cityID
	 */
	public String getCityID() {
		return cityID;
	}

	/**
	 * @param cityID the cityID to set
	 */
	public void setCityID(String cityID) {
		this.cityID = cityID;
	}

	/**
	 * @return the city
	 */
	public ICity getCity() {
		return city;
	}

	/**
	 * @param city the city to set
	 */
	public void setCity(ICity city) {
		this.city = city;
	}

	/**
	 * @return the rootCityList
	 */
	public List<ICity> getRootCityList() {
		return rootCityList;
	}

	/**
	 * @param rootCityList the rootCityList to set
	 */
	public void setRootCityList(List<ICity> rootCityList) {
		this.rootCityList = rootCityList;
	}

	/**
	 * @return the childCityList
	 */
	public List<ICity> getChildCityList() {
		return childCityList;
	}

	/**
	 * @param childCityList the childCityList to set
	 */
	public void setChildCityList(List<ICity> childCityList) {
		this.childCityList = childCityList;
	}
	
}
